;(function () {
    $.fn.extend({
        /**
         * 验证规范
         */
        checkFormat() {
            $.validator.addMethod('checkUser', function (value, element, param) {
                let reg = /^[a-z]\w+$/i;
                return reg.test(value);
            });
            $(this).validate({
                //自定义验证方法
                //规则
                rules: {
                    //用户名
                    username: {
                        required: true,
                        rangelength: [6, 18],
                        checkUser: true,
                        remote: './account/checkName'
                    },
                    //密码
                    password: {
                        required: true,
                        rangelength: [6, 20]
                    },
                    passwordTwo: {
                        equalTo: '#password'
                    },
                    //昵称
                    nickname: {
                        required: true,
                        rangelength: [2, 10]
                    }
                },
                //提示的消息
                messages: {
                    //用户名
                    username: {
                        required: '用户名不能为空',
                        rangelength: '用户名必须是{0}-{1}位',
                        checkUser: '用户名以字母开头,不能包含特殊字符',
                        remote: '该用户名已注册'
                    },
                    password: {
                        required: '密码不能为空',
                        rangelength: '密码必须是{0}-{1}位',
                    },
                    passwordTwo: {
                        equalTo: '两次输入的密码不一致'
                    },
                    nickname: {
                        required: '昵称不能为空',
                        rangelength: '昵称必须是{0}-{1}位'
                    }
                },
                //就是表单提交
                submitHandler() {
                    //将数据提交到服务器
                    $.ajax({
                        url: './account/reg',
                        type: 'post',
                        data: $('#register').serialize()
                    }).then(function (response) {
                        if (response.status === 200) {
                            layer.msg('注册成功')
                            setTimeout(function () {
                                location.href = "login.html";
                            }, 1000)
                        } else {
                            layer.msg('注册失败,' + response.msg);
                        }
                    })
                    return false;
                }
            });
        }
    });
})()

$(function () {
    $('#register').checkFormat();
})